using System.Collections;
using System.Text.RegularExpressions;
using TMPro;
using UnityEngine;

public class TRB_FlashbackSequence_Chatbox : MonoBehaviour
{
	[SerializeField]
	private TextMeshPro TextUI;

	private string CurrentText = "";

	public int CurrentTextIndex;

	public int CurrentAdditionalTextIndex;

	public CHATBOXTEXT storedchatboxtext;

	public bool ChatIsCurrentlyRunning;

	public bool AllowInput = true;

	private int Text_SubtractedRichText;

	private AudioClip currentVoiceClip;

	public float CurrentTextSpeedMultiplier = 1f;

	[SerializeField]
	private TMP_StyleSheet StyleSheet;

	public TMP_FontAsset DefaultFont;

	public TMP_FontAsset DyslexicFont;

	public void RunText(CHATBOXTEXT Chatbox, int individualindex, int index)
	{
		StopCoroutine("PlayText");
		ChatIsCurrentlyRunning = true;
		CurrentTextIndex = individualindex;
		CurrentAdditionalTextIndex = index;
		TextUI.color = Color.white;
		Text_SubtractedRichText = 0;
		if (Chatbox.Textboxes[index].Character[CurrentTextIndex] == null || Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterSound == null)
		{
			currentVoiceClip = null;
		}
		else
		{
			currentVoiceClip = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterSound;
		}
		TextUI.enabled = true;
		TextUI.text = "";
		storedchatboxtext = Chatbox;
		TextUI.font = DefaultFont;
		TextUI.textStyle = StyleSheet.GetStyle(1867431062);
		TextUI.extraPadding = true;
		if (PlayerPrefs.GetInt("Setting_DyslexicText", 0) == 1)
		{
			TextUI.font = DyslexicFont;
			TextUI.textStyle = StyleSheet.GetStyle(1867431062);
			TextUI.extraPadding = false;
		}
		else if (PlayerPrefs.GetInt("Setting_NoFont", 0) == 0 && Chatbox.Textboxes[index].Character[CurrentTextIndex] != null && Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFont != null)
		{
			if (Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFont != null)
			{
				TextUI.font = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFont;
			}
			TextUI.textStyle = StyleSheet.GetStyle((int)Chatbox.Textboxes[index].Character[CurrentTextIndex].TextStyleSheet);
		}
		StartCoroutine("PlayText");
	}

	public void EndText()
	{
		TextUI.enabled = false;
		storedchatboxtext = null;
		CurrentText = "";
		Text_SubtractedRichText = 0;
		CurrentTextIndex = 0;
		CurrentAdditionalTextIndex = 0;
		StopCoroutine("PlayText");
		ChatIsCurrentlyRunning = false;
	}

	private IEnumerator PlayText()
	{
		string StoredText = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex];
		StoredText = StoredText.Replace("’", "'");
		StoredText = StoredText.Replace("£", "*");
		Debug.LogWarning(StoredText);
		TextUI.text = StoredText;
		CurrentText = StoredText;
		TextUI.maxVisibleCharacters = 0;
		int MaxVisibleCharacters = 0;
		Text_SubtractedRichText = 0;
		bool ForcedFinishText = false;
		int messageCharLength = StoredText.Length;
		char[] messageCharacters = StoredText.ToCharArray();
		if (storedchatboxtext != null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action.Length != 0 && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex] != null && !storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex].RunActionOnChatEnd)
		{
			CHATBOXACTION cHATBOXACTION = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex];
			if (cHATBOXACTION.PlaySound && cHATBOXACTION.PossibleSounds.Length != 0)
			{
				CutsceneUtils.PlaySound(cHATBOXACTION.PossibleSounds[Random.Range(0, cHATBOXACTION.PossibleSounds.Length)]);
			}
			GameObject gameObject = GameObject.Find(cHATBOXACTION.TargetComponentGameObjectName);
			if (cHATBOXACTION.RunComponentFunction && gameObject != null && cHATBOXACTION.FunctionName != null)
			{
				string targetComponentClassname = cHATBOXACTION.TargetComponentClassname;
				Component component = gameObject.GetComponent(targetComponentClassname);
				if (component != null)
				{
					if (component.GetType().GetMethod(cHATBOXACTION.FunctionName) != null)
					{
						component.GetType().GetMethod(cHATBOXACTION.FunctionName).Invoke(component, null);
					}
					else
					{
						MonoBehaviour.print("did you forget to make the method public?");
					}
				}
				else
				{
					MonoBehaviour.print("Couldn't find Component named: " + targetComponentClassname);
				}
			}
		}
		while (MaxVisibleCharacters < messageCharLength)
		{
			if (messageCharacters[MaxVisibleCharacters].ToString() == " ")
			{
				MaxVisibleCharacters++;
				yield return new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters] == '<')
			{
				int num = MaxVisibleCharacters;
				int num2 = StoredText.IndexOf('>', num);
				if (num2 != -1)
				{
					int num3 = num2 - num + 1;
					MaxVisibleCharacters += num3;
					Text_SubtractedRichText += num3;
				}
			}
			else if (messageCharacters[MaxVisibleCharacters].ToString() == "(")
			{
				MaxVisibleCharacters++;
				yield return new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (messageCharacters[MaxVisibleCharacters].ToString() == ")")
			{
				MaxVisibleCharacters++;
				yield return new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == "@")
			{
				StoredText = StoredText.Remove(MaxVisibleCharacters, 1);
				TextUI.text = StoredText;
				CurrentText = StoredText;
				messageCharacters = StoredText.ToCharArray();
				messageCharLength = StoredText.Length;
				yield return new WaitForSeconds(0.125f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == "#")
			{
				StoredText = StoredText.Remove(MaxVisibleCharacters, 1);
				TextUI.text = StoredText;
				CurrentText = StoredText;
				messageCharacters = StoredText.ToCharArray();
				messageCharLength = StoredText.Length;
				yield return new WaitForSeconds(0.5f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == ";")
			{
				yield return new WaitForSeconds(0.2f * CurrentTextSpeedMultiplier);
				StoredText = RemoveSelectedCharacter(StoredText, MaxVisibleCharacters);
				StoredText = AddSelectedCharacter(StoredText, MaxVisibleCharacters, "\n");
				TextUI.text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				MaxVisibleCharacters++;
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == "~")
			{
				yield return new WaitForSeconds(0.2f * CurrentTextSpeedMultiplier);
				StoredText = RemoveSelectedCharacter(StoredText, MaxVisibleCharacters);
				StoredText = AddSelectedCharacter(StoredText, MaxVisibleCharacters, "\n");
				TextUI.text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				MaxVisibleCharacters++;
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == ",")
			{
				MaxVisibleCharacters++;
				TextUI.maxVisibleCharacters = MaxVisibleCharacters - Text_SubtractedRichText;
				yield return new WaitForSeconds(0.165f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == ".")
			{
				MaxVisibleCharacters++;
				yield return new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (!ForcedFinishText)
			{
				MaxVisibleCharacters++;
				CutsceneUtils.PlaySound(currentVoiceClip, CutsceneUtils.DRH_MixerChannels.Dialogue);
				yield return new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else
			{
				MaxVisibleCharacters++;
			}
			TextUI.maxVisibleCharacters = MaxVisibleCharacters - Text_SubtractedRichText;
		}
	}

	private string RemoveSelectedCharacter(string text, int Index)
	{
		return text.Remove(Index, 1);
	}

	private string AddSelectedCharacter(string text, int Index, string Insert)
	{
		return text.Insert(Index, Insert);
	}

	public int CountOfStringInText(string TargetText, char Counter)
	{
		int num = 0;
		bool flag = false;
		char[] array = TargetText.ToCharArray();
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i] == Counter)
			{
				flag = true;
			}
			else if (false)
			{
				num++;
			}
		}
		return num;
	}

	public static string RemoveRichTextTags(string input)
	{
		string pattern = "<.*?>";
		return Regex.Replace(input, pattern, "");
	}

	public int CountRichTextTagCharacters(string input)
	{
		string pattern = "<.*?>";
		MatchCollection matchCollection = Regex.Matches(input, pattern);
		int num = 0;
		foreach (Match item in matchCollection)
		{
			num += item.Length;
		}
		return num;
	}
}
